package com.itheima.reggie.Filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.common.ThreadContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
编写全局异常拦截器时,不要忘记在springboot项目中的启动类上加上@ServletComponentScan注解,用来扫描拦截器的
这个格式全局拦截器用于springboot项目

*/
@Slf4j
@WebFilter(filterName = "checkloginfilter", urlPatterns = "/*")
public class GlobalFilter implements Filter {
    private static final String[] stringPath = new String[]{
            "/backend/**",
            "/front/**",
            "/employee/login",
            "/employee/logout",
            "/user/sendMsg",
            "/user/login",
            "/doc.html",
            "/webjars/**",
            "/swagger-resources",
            "/v2/api-docs"
    };

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {



        HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse response1 = (HttpServletResponse) response;

        System.out.println("Login UserId: "+ request1.getSession().getAttribute("userId"));


        String requestURI = request1.getRequestURI();
        log.info("请求路径" + requestURI);
        boolean result = checkPath(stringPath, requestURI);
        log.info("路径匹配得结果" + result);


        //判断,匹配上放行
        if (result) {
            chain.doFilter(request1, response1);
            return;
        }
        //没匹配上,但是后台用户登录了,放行
        if (request1.getSession().getAttribute("employeeId") != null) {
            //把数据存放到线程容器里
            ThreadContext.set((Long) request1.getSession().getAttribute("employeeId"));
            chain.doFilter(request1, response1);
            ThreadContext.remove();
            return;
        }
         //前端用户
        if (request1.getSession().getAttribute("userId") != null) {
            //把数据存放到线程容器里
            ThreadContext.set((Long) request1.getSession().getAttribute("userId"));
            chain.doFilter(request1, response1);
            ThreadContext.remove();
            return;
        }
        //没匹配上,还没登录,返回没登陆的信息
        response1.getWriter().write(JSON.toJSONString(Result.error("NOTLOGIN")));
    }





    /***
     *@Description：校验访问路径的方法
     *@Param：String[] stringPath, String requsetUri
     *@return：result
     *@Author：lsp
     *@Date：*/


    public boolean checkPath(String[] stringPath, String requsetUri) {
        boolean result = false;
        AntPathMatcher checkPath = new AntPathMatcher();
        for (String s : stringPath) {
            //如果匹配上就说明可以 赋值为true;
            result = checkPath.match(s, requsetUri);
            if (result) {
                return true;
            }
        }

        //循环完,没有匹配上,返回false,说明需要校验
        return result;
    }

}



